<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">卡号</label>
        <el-input v-model.trim="query.cardNo" clearable placeholder="卡号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">学习卡状态</label>
        <el-select
          v-model="query.cardStatus"
          clearable
          size="small"
          placeholder="学习卡状态"
          class="filter-item"
          style="width: 145px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in dict.card_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">学习卡类型</label>
        <el-select
          v-model="query.cardType"
          clearable
          size="small"
          placeholder="学习卡类型"
          class="filter-item"
          style="width: 145px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in dict.card_type"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">客户电话</label>
        <el-input v-model.trim="query.customerPhone" clearable placeholder="客户电话" style="width: 165px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">生成时间</label>
        <el-date-picker
          v-model="query.createTime"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="filter-item"
          style="width: 215px;"
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00', '23:59:59']"
          @keyup.enter.native="crud.toQuery"
        />
        <!-- <date-range-picker v-model="query.createTime" class="filter-item" placeholder="生成时间" style="width: 215px;" @keyup.enter.native="crud.toQuery" /> -->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />

      <!--表单组件 新增 修改-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="800px">
        <el-form ref="form" :model="form" :rules="rules" :inline="crud.status.add ===0?true:false" size="small" label-width="120px">

          <el-form-item v-if="crud.status.add ===0" label="编号">
            <el-input v-model="form.cardId" style="width: 240px;" disabled />
          </el-form-item>
          <el-form-item v-if="crud.status.add ===0" label="卡号">
            <el-input v-model="form.cardNo" style="width: 240px;" disabled />
          </el-form-item>
          <!--          <el-form-item v-if="crud.status.add ===0" label="密码">
            <el-input v-model="form.cardPass" style="width: 240px;" disabled />
          </el-form-item>
 -->
          <template v-if="crud.status.add ===0">
            <el-form-item label="学习卡类型">
              <el-select v-model="form.cardType" filterable disabled style="width: 240px;">
                <el-option
                  v-for="item in dict.card_type"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </template>
          <template v-else>
            <el-form-item label="学习卡类型" prop="cardType">
              <el-select v-model="form.cardType" filterable placeholder="请选择" style="width: 570px;">
                <el-option
                  v-for="item in dict.card_type"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </template>

          <!-- <el-form-item v-if="crud.status.add >0" label="学习卡数量" prop="count"> -->
          <template v-if="crud.status.add ===0">
            <el-form-item label="学习卡数量(张)">
              <el-input-number v-model="form.count" style="width: 240px;" :controls="false" :min="1" disabled />
            </el-form-item>
          </template>
          <template v-else>
            <el-form-item label="学习卡数量(张)" prop="count">
              <el-input-number v-model="form.count" style="width: 570px;" :controls="false" :min="1" :max="999999999" placeholder="请输入要批量生成的学习卡数量" />
            </el-form-item>
          </template>

          <el-form-item v-if="crud.status.add ===0" label="生成时间">
            <el-date-picker v-model="form.createTime" type="datetime" style="width: 240px;" disabled />
          </el-form-item>
          <template v-if="crud.status.add ===0">
            <el-form-item label="有效期限(年)">
              <el-select v-model="form.validTime" style="width: 240px" disabled>
                <el-option
                  v-for="item in validTimes"
                  :key="item"
                  :label="item"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </template>
          <template v-else>
            <el-form-item label="有效期限(年)" prop="validTime">
              <el-select v-model="form.validTime" style="width: 400px" placeholder="请选择有效期限" @change="validTimeChange($event)">
                <el-option
                  v-for="item in validTimes"
                  :key="item"
                  :label="item"
                  :value="item"
                />
              </el-select>
              <span v-show="validTimeLabel" style="margin-left: 10px;">有效期限到:{{ validTimeLabel }}</span>
            </el-form-item>
          </template>

          <el-form-item v-if="crud.status.add ===0" label="充值时间">
            <el-date-picker v-model="form.refillTime" type="datetime" style="width: 240px;" disabled />
          </el-form-item>
          <el-form-item v-if="crud.status.add ===0" label="学习卡状态">
            <el-select v-model="form.cardStatus" filterable placeholder="请选择" style="width: 240px;" disabled>
              <el-option
                v-for="item in dict.card_status"
                :key="item.id"
                :label="item.label"
                :value="item.value"
                disabled
              />
            </el-select>
          </el-form-item>
          <template v-if="crud.status.add ===0">
            <el-form-item label="面值(元)">
              <el-input v-model="form.cardAmount" type="number" style="width: 240px;" disabled />
            </el-form-item>
          </template>
          <template v-else>

            <el-form-item label="面值(元)" prop="cardAmount">
              <el-input v-model="form.cardAmount" type="number" style="width: 570px;" />
            </el-form-item>
          </template>

          <el-form-item v-if="crud.status.add ===0" label="充值客户">
            <el-input v-model="form.customerName" style="width: 240px;" disabled />
          </el-form-item>
          <el-form-item v-if="crud.status.add ===0" label="客户电话">
            <el-input v-model="form.customerPhone" style="width: 240px;" disabled />
          </el-form-item>
          <el-form-item v-if="crud.status.add ===0" label="备注">
            <el-input v-model="form.remark" :rows="3" type="textarea" style="width: 240px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>

      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <!-- <el-table-column prop="cardId" label="编号" /> -->
        <el-table-column prop="cardNo" label="卡号" align="center" />
        <!-- <el-table-column prop="cardPass" label="密码" align="center" /> -->
        <el-table-column prop="cardType" label="学习卡类型" align="center">
          <template slot-scope="scope">
            {{ dict.label.card_type[scope.row.cardType] }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="生成时间" align="center">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="validTime" label="有效期限" align="center" />
        <el-table-column prop="cardStatus" label="学习卡状态" align="center">
          <template slot-scope="scope">
            {{ dict.label.card_status[scope.row.cardStatus] }}
          </template>
        </el-table-column>
        <el-table-column prop="cardAmount" label="面值(元)" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.cardAmount /100 }}</span>
          </template>

        </el-table-column>
        <el-table-column prop="updateTime" label="出库时间" align="center">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="refillTime" label="充值时间" align="center">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.refillTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="customerName" label="充值客户" align="center" />
        <el-table-column prop="customerPhone" label="客户电话" align="center" />

        <!-- <el-table-column v-permission="['admin','cardRefill:edit','cardRefill:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column> -->
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudCardRefill from '@/api/cardRefill'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'

const defaultForm = { cardId: null, count: 1, cardNo: null, cardPass: null, cardType: 1, createTime: null, validTime: '3', refillTime: null, cardStatus: null, cardAmount: null, customerId: null, customerName: null, customerPhone: null, remark: null }
export default {
  name: 'CardRefill',
  components: { pagination, crudOperation, rrOperation, DateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['card_type', 'card_status'],
  cruds() {
    return CRUD({ title: '学习卡', url: 'api/cardRefill', idField: 'cardId', sort: 'cardId,desc', crudMethod: { ...crudCardRefill },
      optShow: {
        add: true,
        del: false,
        reset: true,
        edit: true,
        download: true
      }})
  },
  data() {
    const validateLength = (rule, value, callback) => {
      if (value.length > 25) {
        callback(new Error('长度不能超过25个字符，请重新输入'))
      } else {
        callback()
      }
    }
    return {
      validTimeLabel: '',
      validTimes: ['1', '2', '3'],
      permission: {
        add: ['admin', 'cardRefill:add'],
        edit: ['admin', 'cardRefill:edit'],
        del: ['admin', 'cardRefill:del']
      },
      validShow: {
        true: '',
        false: ''
      },
      rules: {
        cardType: [
          { required: true, message: '请选择学习卡类型', trigger: 'blur' }
        ],
        count: [
          { required: true, message: '请输入要批量生成的学习卡数量', trigger: 'blur' }
        ],
        validTime: [
          { required: true, message: '请输入有效期限', trigger: 'blur' }
        ],
        cardAmount: [
          { required: true, message: '请输入面值', trigger: 'blur' },
          { trigger: 'blur', validator: validateLength }
        ]
      },
      queryTypeOptions: [
        { key: 'cardNo', display_name: '卡号' },
        { key: 'cardStatus', display_name: '学习卡状态' },
        { key: 'customerPhone', display_name: '客户电话' }
      ]
    }
  },
  created() {
    // this.currentDateLater(parseInt(3))
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh](a, b) {
      return true
    },
    // 新增之前
    [CRUD.HOOK.beforeToAdd](a, b) {
      // this.validTimeLabel = ''
      this.currentDateLater(parseInt(b.validTime))
      return true
    },

    [CRUD.HOOK.beforeDelete](a, b) {
      return true
    },

    validTimeChange(value) {
      this.currentDateLater(parseInt(value))
    },

    currentDateLater(number) {
      var date = new Date()
      var y = date.getFullYear() + number
      var m = date.getMonth()
      var d = date.getDate()
      this.validTimeLabel = y + '-' + m + '-' + d
    },

    [CRUD.HOOK.beforeToEdit](a, b) {
      // alert('' + this.dict.label.card_status[this.form.cardStatus])
      // alert(this.form.cardStatus)
      this.form.cardStatus = this.form.cardStatus + ''
      // alert(b.cardStatus)
      // this.$forceUpdate()
      // alert(b.cardType)
      b.cardAmount = b.cardAmount / 100
      return true
    },
    [CRUD.HOOK.afterToCU](crud, form) {
      form.cardType = form.cardType.toString()
    }
  }
}
</script>

<style scoped lang="scss">
  .head-container {
    ::v-deep .el-input-number.is-without-controls .el-input__inner {
      text-align: left;
    }

    /** 去除input输入框样式 */
    ::v-deep  input[type=number]::-webkit-inner-spin-button {
      -webkit-appearance: none;
    }
	::v-deep input[type="number"]{
	    -moz-appearance:textfield;
	}
  }
</style>
